/**
 * 
 * @author ryannikolaidis
 *
 * This provides for timing of events, it runs the 'run thread' function in Score
 *
 */
public class Metro implements Runnable { 
	public Object[] noteArray = new Object[3];
	public void run()
	{
//		Main.score.headbang(true);
		int count = 10;
		while(Main.score.metroOn)
		{
			long startTime=System.nanoTime();
			long triggerTime=startTime+(long)1000000*Main.quarter;
			long currentTime=System.nanoTime();
			
			while (currentTime<triggerTime&&Main.score.metroOn)
			{
				triggerTime=startTime+(long)1000000*Main.quarter;
				currentTime=System.nanoTime();
				for (int i=0;i<Main.score.noteScheduler[0].length;i++)
				{
					if (Main.score.noteScheduler[0][i]==1&&(currentTime-startTime)/1000000>Main.score.noteScheduler[1][i])
					{
						Main.score.noteScheduler[0][i]=0;
						noteOut(Main.score.noteScheduler[2][i],(double) Main.score.noteScheduler[3][i], Main.score.noteScheduler[4][i]);
						Main.score.noteScheduler[0][i]=0;
					}
				}
			}
			Main.score.clearNoteScheduler();
			count=count+1;			
			Main.score.scheduleEvents(count);

		}
	}
	public void noteOut(int note, double velocity,int arm){
		int vel = (int) (velocity*Main.velocity/70.);
		
		if (arm==2&&(note%12==1||note%12==3||note%12==6||note%12==8||note%12==10)) {
			vel*=.6;
		}
		
		if (arm==0) {
			vel=127;
		}
		
		System.out.println("note out = "+note);
		noteArray[0] = arm;
		noteArray[1] = note;
		noteArray[2] = (int) vel;
		
		OSC.main(noteArray,"/directControl");
		int[] intNoteArray=new int[3];
		intNoteArray[0]=(Integer) noteArray[0];
		intNoteArray[1]=(Integer) noteArray[1];
	}	
}
